package org.bemuv;

import com.parse.ParseException;
import com.parse.ParseFile;
import com.parse.ParseObject;
import com.parse.ParseUser;
import com.parse.SaveCallback;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class AddItemActivity extends Activity {
	
	private Button btn_additem;
	private Button btn_save;
	private EditText txt_nameitem;
	private EditText txt_descrip;
	private EditText txt_price;
	private EditText txt_flags;
	private boolean pic=false;
	private boolean info=false;
	private String current="";
	
	

	@Override
	protected void onCreate(Bundle savedInstanceState){	
	super.onCreate(savedInstanceState);
	setContentView(R.layout.activity_add_item);
	
   

	
    	
    	
    	
    	btn_additem = (Button)findViewById(R.id.btn_additem); //Boton guardar item;
    	btn_additem.setOnClickListener(new OnClickListener() {       		
       		@Override
       		public void onClick(View v) {
       			add();
       	
       		}
       	});
    	final BemuvApplication state = ((BemuvApplication)getApplicationContext());
		
    		btn_save = (Button)findViewById(R.id.btn_save); //Escucha finalizar
        	btn_save.setOnClickListener(new OnClickListener() {       		
           		@Override
           		public void onClick(View v) {
           			

           			
           			
           			if(state.getState4()==true){Intent i = new Intent(getApplicationContext(),
        					HomeActivity.class);
        			startActivity(i);} else{
        				Toast.makeText(getApplicationContext(),
	        					"Debes agregar almenos 1 Item", Toast.LENGTH_SHORT).show();
        				
        			}
           			
		
           		}
           	});
        	}



	
	
	public void add() {
		

		txt_nameitem = (EditText) findViewById(R.id.txt_nameitem);
		txt_descrip = (EditText) findViewById(R.id.txt_descrip);
		txt_flags = (EditText) findViewById(R.id.txt_flags);
		txt_price = (EditText) findViewById(R.id.txt_price);
		
		 boolean error = false;

		if (txt_nameitem.getText().toString().length() == 0) {
			txt_nameitem.setError("Requerido");
			error = true;
		}
		if (txt_descrip.getText().toString().length() == 0) {
			txt_descrip.setError("Requerida");
			error = true;
		}
		if (txt_flags.getText().toString().length() == 0) {
			txt_flags.setError("Requerido");
			error = true;
		}
		
		if (txt_price.getText().toString().length() == 0) {
			txt_price.setError("Requerido");
			error = true;
		}


	
		if (!error) {   //If todo esta bien ---------
			BemuvApplication state = ((BemuvApplication)getApplicationContext());
				state.setState4(true);	//Variable global True
			ParseUser currentUser = ParseUser.getCurrentUser();
			current = (String) currentUser.get("username");

			final ParseObject addShop = new ParseObject("ObjectSale");
			addShop.put("nameObject", txt_nameitem.getText().toString());
			addShop.put("descriptionObject", txt_descrip.getText().toString());
			addShop.put("categoria", txt_flags.getText().toString());
			addShop.put("priceObject", txt_price.getText().toString());
			addShop.put("nameObjectCaps", txt_nameitem.getText().toString().toUpperCase().trim());
			addShop.put("userID", current.toString()); //guardo el ID del usuario 

			addShop.saveInBackground(new SaveCallback() {
		            public void done(ParseException e) {
		                if (e == null) {
		                	String s = addShop.getObjectId(); //Obtener el ID del objeto que acabo de guardar
		                	
		                	Toast.makeText(getApplicationContext(),
		        					"Se guardo satisfactoriamente", Toast.LENGTH_SHORT).show();
		                	
		                	BemuvApplication appState = ((BemuvApplication)getApplicationContext());
		                    appState.setState1(s);	//Lo guardo en una variable "global"

		                } else {
		                 
		                }
		            }
		        });

			txt_flags.setText("");
			txt_descrip.setText("");
			txt_price.setText("");
			txt_nameitem.setText("");
	
			Toast.makeText(getApplicationContext(),
					"Ahora agrega una Imagen", Toast.LENGTH_SHORT).show();
			
			Intent i = new Intent(getApplicationContext(),
					CameraActivity.class);
			startActivity(i);
			
			}
	
		
			
	}  	
		

}

